package iip

import (
    "context"

    "github.com/gin-gonic/gin"
    "gorm.io/gorm"
    modeldict "iip/dictionary/model"
    "iip/iip/model"
)

func (s *IIP) json_error(c *gin.Context, err string) {
    c.JSON(200, gin.H{
        "code":    200,
        "message": err,
    })
}

func (s *IIP) json_ok(c *gin.Context) {
    c.JSON(200, gin.H{
        "code":    200,
        "message": "",
    })
}

func (s *IIP) json_data(c *gin.Context, d interface{}) {
    c.JSON(200, gin.H{
        "code":    200,
        "message": "",
        "data":    d,
    })
}

func (s *IIP) json_paged_data(c *gin.Context, total int64, page, pagesize int, d interface{}) {
    c.JSON(200, gin.H{
        "code":     200,
        "message":  "",
        "total":    total,
        "page":     page,
        "pagesize": pagesize,
        "data":     d,
    })
}

func (s *IIP) ctx() context.Context {
    return context.Background()
}

func (s *IIP) gorm() *gorm.DB {

    return s.db.WithContext(context.Background())
}

func (s *IIP) GetWeightRecordByID(id int64) (*model.WeightRecord, error) {
    var row model.WeightRecord
    if err := s.gorm().Where("id = ?", id).First(&row).Error; err != nil {
        return &model.WeightRecord{}, err
    }
    return &row, nil
}









func (s *IIP) WeightRecordFull(row *model.WeightRecord, truck *modeldict.DictTruck, wb *modeldict.DictWeighbridge) interface{} {
    rsp := &struct {
        model.WeightRecord
        Truck       modeldict.DictTruck       `json:"truck"`
        Weighbridge modeldict.DictWeighbridge `json:"weighbridge"`
    }{}
    rsp.WeightRecord = *row
    rsp.Truck = *truck
    rsp.Weighbridge = *wb
    return rsp
}

func (s *IIP) client_full(c *model.Client, loc *modeldict.DictLocation) interface{} {
    rsp := &struct {
        model.Client
        Location *modeldict.DictLocation `json:"location"`
    }{}
    rsp.Client = *c
    rsp.Location = loc
    return rsp
}

func (s *IIP) GetClientByID(id int64) (*model.Client, error) {
    var row model.Client
    if err := s.gorm().Where("id = ?", id).First(&row).Error; err != nil {
        return &model.Client{}, err
    }
    return &row, nil
}
